//
//  AD_Environment.m
//  ActiveDeck
//
//  Created by Michael Cohen on 10/31/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "AD_Environment.h"

#define ACTIVEDECKPASS @"ActiveDeckPass"
#define ACTIVEDECKUSER @"ActiveDeckUser"

@implementation AD_Environment



@synthesize password, username, simpleUsername, authNode;

-(AD_Environment*)init{
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    if([prefs stringForKey:ACTIVEDECKUSER].length > 0 && [prefs stringForKey:ACTIVEDECKPASS].length > 0){
        username = [prefs stringForKey:ACTIVEDECKUSER];
        password = [prefs stringForKey:ACTIVEDECKPASS];
        
        NSRange atSymbol = [username rangeOfString:@"@"];
        NSRange rangeToKeep = NSMakeRange(0, atSymbol.location);
        
        NSMutableString *theName = [[NSMutableString alloc] initWithString:[username substringWithRange:rangeToKeep]];
        
        simpleUsername = theName;
    }
    
    return self;
}

//check if we have the username and password
-(BOOL)hasLogin{
    if(username.length > 0 && password.length > 0)
        return YES;
    else
        return NO;
}

//return the Authentication XML
-(NSString*)getAuthentication{
    if([self hasLogin]){
        GDataXMLElement *Authentication = [GDataXMLNode elementWithName:@"Authentication"];
        GDataXMLNode *UserName = [GDataXMLNode attributeWithName:@"UserName" stringValue:username];
        GDataXMLNode *Password = [GDataXMLNode attributeWithName:@"Password" stringValue:password];
        GDataXMLNode *Plaintext = [GDataXMLNode attributeWithName:@"Plaintext" stringValue:@"True"];
        
        [Authentication addChild:UserName];
        [Authentication addChild:Password];    
        [Authentication addChild:Plaintext];
        
        GDataXMLDocument *document = [[[GDataXMLDocument alloc] initWithRootElement:Authentication] autorelease];
        NSData	*xData = document.XMLData;
        
        
        authNode = [[GDataXMLElement alloc] init];
        authNode = [Authentication copy];
        
        return [[NSString alloc] initWithData:xData encoding:NSUTF8StringEncoding];
    }
    
    return @"";
}

//set the username and password
-(void)setAuthenticationUsername:(NSString *)name AndPassword :(NSString *)psswd{
    username = name;
    password = psswd;
    
    NSRange atSymbol = [username rangeOfString:@"@"];
    NSRange rangeToKeep = NSMakeRange(0, atSymbol.location);
    
    NSMutableString *theName = [[NSMutableString alloc] initWithString:[username substringWithRange:rangeToKeep]];
    
    simpleUsername = theName;
    
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:username forKey:ACTIVEDECKUSER];
    [prefs setObject:password forKey:ACTIVEDECKPASS];
    [prefs synchronize];
}

/// <summary>
/// Clear out any environment/authentication information
/// </summary>
-(void)clearEnvironment{
    username = @"";
    password = @"";
    simpleUsername = @"";
    authNode = nil;
    
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs removeObjectForKey:ACTIVEDECKUSER];
    [prefs removeObjectForKey:ACTIVEDECKPASS];
    [prefs synchronize];
}
@end
